import type { ISettingsState, IUserPreferences, IDisplaySettings } from './settingsStore';
import type { ThemeMode } from '../../types/common/base';

/**
 * 设置选择器函数集合
 * @description 提供设置状态的选择器函数，用于获取和计算派生状态
 */
export const settingsSelectors = {
  // 主题相关选择器
  
  /**
   * 获取当前主题模式
   * @param state 设置状态
   * @returns 当前主题模式
   */
  getThemeMode: (state: ISettingsState): ThemeMode => state.themeMode,

  /**
   * 获取实际应用的主题模式
   * @description 如果是auto模式，返回系统检测的主题；否则返回设置的主题
   * @param state 设置状态
   * @returns 实际应用的主题模式
   */
  getEffectiveTheme: (state: ISettingsState): 'light' | 'dark' => {
    if (state.themeMode === 'auto') {
      return state.detectSystemTheme() === 'dark' ? 'dark' : 'light';
    }
    return state.themeMode as 'light' | 'dark';
  },

  /**
   * 检查是否为深色主题
   * @param state 设置状态
   * @returns 是否为深色主题
   */
  isDarkTheme: (state: ISettingsState): boolean => {
    return settingsSelectors.getEffectiveTheme(state) === 'dark';
  },

  /**
   * 检查是否跟随系统主题
   * @param state 设置状态
   * @returns 是否跟随系统主题
   */
  isFollowingSystemTheme: (state: ISettingsState): boolean => state.followSystemTheme,

  /**
   * 获取自定义主题色
   * @param state 设置状态
   * @returns 自定义主题色
   */
  getCustomPrimaryColor: (state: ISettingsState): string => state.customPrimaryColor,

  /**
   * 检查是否使用自定义主题色
   * @param state 设置状态
   * @returns 是否使用自定义主题色
   */
  isUsingCustomColor: (state: ISettingsState): boolean => {
    return state.customPrimaryColor !== '#1890ff';
  },

  // 用户偏好设置选择器

  /**
   * 获取用户偏好设置
   * @param state 设置状态
   * @returns 用户偏好设置
   */
  getUserPreferences: (state: ISettingsState): IUserPreferences => state.userPreferences,

  /**
   * 获取语言设置
   * @param state 设置状态
   * @returns 语言设置
   */
  getLanguage: (state: ISettingsState): string => state.userPreferences.language,

  /**
   * 获取时间格式
   * @param state 设置状态
   * @returns 时间格式
   */
  getTimeFormat: (state: ISettingsState): '12h' | '24h' => state.userPreferences.timeFormat,

  /**
   * 获取日期格式
   * @param state 设置状态
   * @returns 日期格式
   */
  getDateFormat: (state: ISettingsState): string => state.userPreferences.dateFormat,

  /**
   * 获取每周开始日
   * @param state 设置状态
   * @returns 每周开始日 (0: 周日, 1: 周一)
   */
  getWeekStartsOn: (state: ISettingsState): 0 | 1 => state.userPreferences.weekStartsOn,

  /**
   * 获取默认课程时长
   * @param state 设置状态
   * @returns 默认课程时长（分钟）
   */
  getDefaultCourseDuration: (state: ISettingsState): number => state.userPreferences.defaultCourseDuration,

  /**
   * 检查是否显示周末
   * @param state 设置状态
   * @returns 是否显示周末
   */
  shouldShowWeekends: (state: ISettingsState): boolean => state.userPreferences.showWeekends,

  /**
   * 检查是否启用声音通知
   * @param state 设置状态
   * @returns 是否启用声音通知
   */
  isSoundNotificationEnabled: (state: ISettingsState): boolean => state.userPreferences.enableSoundNotifications,

  /**
   * 检查是否启用桌面通知
   * @param state 设置状态
   * @returns 是否启用桌面通知
   */
  isDesktopNotificationEnabled: (state: ISettingsState): boolean => state.userPreferences.enableDesktopNotifications,

  /**
   * 获取自动保存间隔
   * @param state 设置状态
   * @returns 自动保存间隔（秒）
   */
  getAutoSaveInterval: (state: ISettingsState): number => state.userPreferences.autoSaveInterval,

  /**
   * 获取数据备份频率
   * @param state 设置状态
   * @returns 数据备份频率（天）
   */
  getBackupFrequency: (state: ISettingsState): number => state.userPreferences.backupFrequency,

  // 显示设置选择器

  /**
   * 获取显示设置
   * @param state 设置状态
   * @returns 显示设置
   */
  getDisplaySettings: (state: ISettingsState): IDisplaySettings => state.displaySettings,

  /**
   * 获取默认课程表视图模式
   * @param state 设置状态
   * @returns 默认课程表视图模式
   */
  getDefaultScheduleView: (state: ISettingsState): 'week' | 'day' | 'month' => state.displaySettings.defaultScheduleView,

  /**
   * 检查是否显示课程颜色
   * @param state 设置状态
   * @returns 是否显示课程颜色
   */
  shouldShowCourseColors: (state: ISettingsState): boolean => state.displaySettings.showCourseColors,

  /**
   * 检查是否显示教师信息
   * @param state 设置状态
   * @returns 是否显示教师信息
   */
  shouldShowTeacherInfo: (state: ISettingsState): boolean => state.displaySettings.showTeacherInfo,

  /**
   * 检查是否显示地点信息
   * @param state 设置状态
   * @returns 是否显示地点信息
   */
  shouldShowLocationInfo: (state: ISettingsState): boolean => state.displaySettings.showLocationInfo,

  /**
   * 获取课程卡片大小
   * @param state 设置状态
   * @returns 课程卡片大小
   */
  getCourseCardSize: (state: ISettingsState): 'small' | 'medium' | 'large' => state.displaySettings.courseCardSize,

  /**
   * 检查是否启用动画效果
   * @param state 设置状态
   * @returns 是否启用动画效果
   */
  isAnimationEnabled: (state: ISettingsState): boolean => state.displaySettings.enableAnimations,

  /**
   * 检查侧边栏是否折叠
   * @param state 设置状态
   * @returns 侧边栏是否折叠
   */
  isSidebarCollapsed: (state: ISettingsState): boolean => state.displaySettings.sidebarCollapsed,

  /**
   * 获取表格每页显示数量
   * @param state 设置状态
   * @returns 表格每页显示数量
   */
  getTablePageSize: (state: ISettingsState): number => state.displaySettings.tablePageSize,

  // UI状态选择器

  /**
   * 检查设置面板是否显示
   * @param state 设置状态
   * @returns 设置面板是否显示
   */
  isSettingsPanelVisible: (state: ISettingsState): boolean => state.showSettingsPanel,

  /**
   * 获取当前活跃的设置标签页
   * @param state 设置状态
   * @returns 当前活跃的设置标签页
   */
  getActiveSettingsTab: (state: ISettingsState): ISettingsState['activeSettingsTab'] => state.activeSettingsTab,

  /**
   * 检查重置确认对话框是否显示
   * @param state 设置状态
   * @returns 重置确认对话框是否显示
   */
  isResetConfirmVisible: (state: ISettingsState): boolean => state.showResetConfirm,

  // 异步状态选择器

  /**
   * 检查是否正在加载
   * @param state 设置状态
   * @returns 是否正在加载
   */
  isLoading: (state: ISettingsState): boolean => state.async.loading,

  /**
   * 获取错误信息
   * @param state 设置状态
   * @returns 错误信息
   */
  getError: (state: ISettingsState): string | null => state.async.error,

  /**
   * 获取最后更新时间
   * @param state 设置状态
   * @returns 最后更新时间戳
   */
  getLastUpdated: (state: ISettingsState): number | null => state.async.lastUpdated,

  // 复合选择器

  /**
   * 获取主题相关的完整配置
   * @param state 设置状态
   * @returns 主题配置对象
   */
  getThemeConfig: (state: ISettingsState) => ({
    mode: settingsSelectors.getThemeMode(state),
    effectiveTheme: settingsSelectors.getEffectiveTheme(state),
    isDark: settingsSelectors.isDarkTheme(state),
    followSystem: settingsSelectors.isFollowingSystemTheme(state),
    primaryColor: settingsSelectors.getCustomPrimaryColor(state),
    isCustomColor: settingsSelectors.isUsingCustomColor(state)
  }),

  /**
   * 获取本地化相关的配置
   * @param state 设置状态
   * @returns 本地化配置对象
   */
  getLocalizationConfig: (state: ISettingsState) => ({
    language: settingsSelectors.getLanguage(state),
    timeFormat: settingsSelectors.getTimeFormat(state),
    dateFormat: settingsSelectors.getDateFormat(state),
    weekStartsOn: settingsSelectors.getWeekStartsOn(state)
  }),

  /**
   * 获取通知相关的配置
   * @param state 设置状态
   * @returns 通知配置对象
   */
  getNotificationConfig: (state: ISettingsState) => ({
    sound: settingsSelectors.isSoundNotificationEnabled(state),
    desktop: settingsSelectors.isDesktopNotificationEnabled(state)
  }),

  /**
   * 获取课程表显示相关的配置
   * @param state 设置状态
   * @returns 课程表显示配置对象
   */
  getScheduleDisplayConfig: (state: ISettingsState) => ({
    defaultView: settingsSelectors.getDefaultScheduleView(state),
    showColors: settingsSelectors.shouldShowCourseColors(state),
    showTeacher: settingsSelectors.shouldShowTeacherInfo(state),
    showLocation: settingsSelectors.shouldShowLocationInfo(state),
    cardSize: settingsSelectors.getCourseCardSize(state),
    showWeekends: settingsSelectors.shouldShowWeekends(state)
  }),

  /**
   * 获取性能相关的配置
   * @param state 设置状态
   * @returns 性能配置对象
   */
  getPerformanceConfig: (state: ISettingsState) => ({
    enableAnimations: settingsSelectors.isAnimationEnabled(state),
    autoSaveInterval: settingsSelectors.getAutoSaveInterval(state),
    tablePageSize: settingsSelectors.getTablePageSize(state)
  }),

  /**
   * 获取设置摘要信息
   * @param state 设置状态
   * @returns 设置摘要信息
   */
  getSettingsSummary: (state: ISettingsState) => state.getSettingsSummary(),

  /**
   * 检查设置是否已修改（与默认值不同）
   * @param state 设置状态
   * @returns 是否已修改设置
   */
  hasModifiedSettings: (state: ISettingsState): boolean => {
    const summary = state.getSettingsSummary();
    return summary.lastUpdated > 0;
  },

  /**
   * 获取当前设置的完整状态快照
   * @param state 设置状态
   * @returns 设置状态快照
   */
  getSettingsSnapshot: (state: ISettingsState) => ({
    theme: settingsSelectors.getThemeConfig(state),
    localization: settingsSelectors.getLocalizationConfig(state),
    notifications: settingsSelectors.getNotificationConfig(state),
    display: settingsSelectors.getScheduleDisplayConfig(state),
    performance: settingsSelectors.getPerformanceConfig(state),
    ui: {
      panelVisible: settingsSelectors.isSettingsPanelVisible(state),
      activeTab: settingsSelectors.getActiveSettingsTab(state),
      resetConfirmVisible: settingsSelectors.isResetConfirmVisible(state)
    },
    async: {
      loading: settingsSelectors.isLoading(state),
      error: settingsSelectors.getError(state),
      lastUpdated: settingsSelectors.getLastUpdated(state)
    }
  })
};

/**
 * 设置状态选择器类型
 * @description 导出选择器函数的类型定义
 */
export type SettingsSelectors = typeof settingsSelectors;